package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.LoginBean;
import com.sikao.app.bean.LoginSubmitBean;
import com.sikao.app.dao.LoginModel;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.ConstantClass;
import com.sikao.app.utils.JSONUtil;

/**
 * 登录
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private EditText accountNumberEt;
	private EditText passwordEt;
	private TextView registerTv;
	private TextView retrievePasswordTv;
	private Button loginBtn;
	private LoginModel loginModel;
	private String activityName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_login);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		activityName = getIntent().getStringExtra("activity");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		accountNumberEt = (EditText) findViewById(R.id.account_number_et);
		passwordEt = (EditText) findViewById(R.id.password_et);
		registerTv = (TextView) findViewById(R.id.register_tv);
		retrievePasswordTv = (TextView) findViewById(R.id.retrieve_password_tv);
		loginBtn = (Button) findViewById(R.id.login_btn);
		//HttpApi.sessionId=null;
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		registerTv.setOnClickListener(this);
		retrievePasswordTv.setOnClickListener(this);
		loginBtn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("登录");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.login_btn:
				login();
				break;
			case R.id.register_tv:
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
				break;
			case R.id.retrieve_password_tv:
				Intent intent1 = new Intent(LoginActivity.this, RetrieveLoginPswActivity.class);
				startActivity(intent1);
				break;
			default:
				break;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (loginModel != null) {
			loginModel.cancelRequest();
		}
	}


	private void login() {
		if (!checkInput()) {
			return;
		}
		LoginSubmitBean bean = new LoginSubmitBean();
		bean.setNameOrPhone(accountNumberEt.getText().toString());
		bean.setPwd(passwordEt.getText().toString());
		if (loginModel == null) {
			loginModel = new LoginModel(this, new OnNetBackListener<LoginBean>() {
				@Override
				public void netBack(LoginBean data) {
					if (data == null) {

					} else {
						if (data.getStatus().equals("1")) {
							Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
							ConstantClass.state = 1;
							ConstantClass.imgUrl = data.getRes().getImgsrc();
							ConstantClass.userName = data.getRes().getName();
							if (data.getRes().getCash() != null) {
								ConstantClass.balanceValue = Float.valueOf(data.getRes().getCash());
							}
							if (data.getRes().getFrozen() != null) {
								ConstantClass.frozenFund = Float.valueOf(data.getRes().getFrozen());
							}
							if (data.getRes().getCredit() != null) {
								ConstantClass.creditValue = Float.valueOf(data.getRes().getCredit());
							}
							if (data.getRes().getCheck_status().equals("2")) {
								ConstantClass.isSeller = true;
							}
							ConstantClass.phoneNumber = data.getRes().getPhone();
							finish();
							if (activityName != null) {
								Intent intent = new Intent();
								if ((activityName.equals(SellerCenterActivity.class.getName()) || activityName.equals(ReleaseGoodsActivity.class.getName())) && !ConstantClass.isSeller) {
									intent.setClassName(LoginActivity.this, CompanyApplyActivity.class.getName());
								} else {
									intent.setClassName(LoginActivity.this, activityName);
								}
								startActivity(intent);
							}
						} else {
							Toast.makeText(LoginActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}
				}

			});
		}
		loginModel.request(JSONUtil.toJSON(bean), true);

	}

	private boolean checkInput() {
		if (TextUtils.isEmpty(accountNumberEt.getText().toString())) {
			Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(passwordEt.getText().toString())) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

}
